package com.sfl.api_backend.controller.auth;

import com.sfl.api_backend.constant.HttpCode;
import com.sfl.api_backend.entity.Users;
import com.sfl.api_backend.service.ILoginService;
import com.sfl.api_backend.utils.Api.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author szc
 * @create 2023/1/29 21:34
 */
@RestController
@RequestMapping("/auth")
@Slf4j
public class AuthController {


    @Resource
    private ILoginService loginService;


    @PostMapping("/signUp")
    public RestResult signUp(@RequestBody Users users) {
        try {
            log.info("User email: {} signed up", users.getEmail());
            return loginService.signUp(users);
        }
        catch (Exception e){
            log.error("User email: {} signed up error", users.getEmail());
            return RestResult.error(HttpCode.UNAUTHORIZED, e.getMessage());
        }
    }

    @PostMapping("/signIn")
    public RestResult signIn(@RequestBody Users users) {
        try{
            log.info("User email: {} signed in", users.getEmail());
            log.info(users.getPassword());


            return loginService.signIn(users);
        }
        catch (Exception e){
            log.error("User {} signed in error", users.getEmail());
            return RestResult.error(HttpCode.UNAUTHORIZED, e.getMessage());
        }
    }

    @RequestMapping("/signOut")
    public RestResult signOut() {
        try{
            return loginService.signOut();
        }
        catch (Exception e){
            return RestResult.error(HttpCode.UNAUTHORIZED, e.getMessage());
        }
    }
}
